package com.lfy.modules.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lfy.modules.blog.domain.vo.index.IndexVO;
import com.lfy.modules.blog.service.IndexService;
import com.lfy.modules.common.domain.R;
import com.lfy.modules.common.domain.entity.UserEntity;
import com.lfy.modules.letter.domain.SiteLetterEntity;
import com.lfy.modules.letter.domain.builder.LetterEnum;
import com.lfy.modules.letter.service.SiteLetterService;
import com.lfy.modules.safety.domain.vo.VisitorLogVO;
import com.lfy.modules.safety.domain.vo.VisitorVO;
import com.lfy.modules.safety.statistics.heartbeat.HeartBeatCache;
import com.lfy.modules.safety.statistics.vistor.VisitorRecordCache;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 首页数据服务实现类
 * @author lfy
 */
@Service
public class IndexServiceImpl implements IndexService {
    @Resource
    private SiteLetterService siteLetterService;
    @Override
    public R<IndexVO> getBlogIndex(UserEntity user) {
        IndexVO indexVO = new IndexVO();
        //今日浏览量
        int visitorCount = VisitorRecordCache.getVisitorCount(user.getUrl());
        indexVO.setVisitorCount(visitorCount);
        //正在浏览博客的实时人数
        int onlineCount = HeartBeatCache.getOnlineCount(user.getUrl());
        indexVO.setOnlineCount(onlineCount);
        //站内信未读数
        int count = this.siteLetterService.count(new LambdaQueryWrapper<SiteLetterEntity>().eq(SiteLetterEntity::getReceiveId, user.getId()).eq(SiteLetterEntity::getIsRead, LetterEnum.ReadType.UN_READ.getCode()));
        indexVO.setNoReadCount(count);
        //7天浏览量折线图统计数据
        List<VisitorVO> yesterdayBetweenSevenCount = VisitorRecordCache.getYesterdayBetweenSevenCount(user.getUrl());
        indexVO.setSevenDaysVisitors(yesterdayBetweenSevenCount);
        return R.success(indexVO);
    }

    @Override
    public R<List<VisitorLogVO>> getLookDetailByDate(UserEntity user, String date) {

        return R.success(VisitorRecordCache.getVisitorLog(user, date));

    }
}
